
package com.the9.C9Alarm;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

import holidaynotifaction.HolidayReceiver;

import java.util.List;

public class boxGameAlarmActivity extends Activity implements RefreshAlarm {
    private final static String TAG                     = "alarm";

    RelativeLayout m_layoutMain;

   // ListView m_alarmList;

    Button m_btStopSleepAlarm;
    RelativeLayout tl,tr,br,bl;
    TextView tvtlname,tvtltime,tvtlring;
    TextView tvblname,tvbltime,tvblring;
    TextView tvtrname,tvtrtime,tvtrring;
    TextView tvbrname,tvbrtime,tvbrring;
    ImageButton blm,tlm,trm,brm;
    ImageView imageView_cirtl,imageView_cirtr,imageView_cirbl,imageView_cirbr;
    private List<AlarmBean> m_listBean;
    

    
//    Button m_btChangeBg;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainm);
        HolidayReceiver.setAlarm(this);
        initUI();
        setListener();
        setUI();
    }

    @Override
    protected void onResume() {
        refreshUI();
        super.onResume();
    }
    private void initUI(){
    	  m_layoutMain = (RelativeLayout) findViewById(R.id.layoutMain);
    	  m_btStopSleepAlarm= (Button) findViewById(R.id.btStop);
    	  
    	  tl =(RelativeLayout) findViewById(R.id.include1);
    	  tvtlname = (TextView) findViewById(R.id.texttlname);
    	  tvtlring = (TextView) findViewById(R.id.texttltime);
          tvtltime = (TextView) findViewById(R.id.texttlring);
          tlm = (ImageButton) findViewById(R.id.imageBttl);
          imageView_cirtl = (ImageView) findViewById(R.id.imageView_cirtl);
          
          bl =(RelativeLayout) findViewById(R.id.include3);
    	  tvblname = (TextView) findViewById(R.id.textblname);
          tvbltime = (TextView) findViewById(R.id.textbltime);
          tvblring = (TextView) findViewById(R.id.textblring);
          blm = (ImageButton) findViewById(R.id.imageBtbl);
          imageView_cirtr = (ImageView) findViewById(R.id.imageView_cirtr);
          
          tr =(RelativeLayout) findViewById(R.id.include2);
    	  tvtrname = (TextView) findViewById(R.id.texttrname);
          tvtrtime = (TextView) findViewById(R.id.texttrtime);
          tvtrring = (TextView) findViewById(R.id.texttrring);
          trm = (ImageButton) findViewById(R.id.imageBttr);
          imageView_cirbl = (ImageView) findViewById(R.id.imageView_cirbl);
          
          br =(RelativeLayout) findViewById(R.id.include4);
    	  tvbrname = (TextView) findViewById(R.id.textbrname);
          tvbrtime = (TextView) findViewById(R.id.textbrtime);
          tvbrring = (TextView) findViewById(R.id.textbrring);
          brm = (ImageButton) findViewById(R.id.imageBtbr);
          imageView_cirbr = (ImageView) findViewById(R.id.imageView_cirbr);
    }
    private void setListener(){
    	 m_btStopSleepAlarm.setOnClickListener(new OnClickListener(){

 			@Override
 			public void onClick(View v) {
 				// TODO Auto-generated method stub
 				 Utils.forceStopSleepAlarm(boxGameAlarmActivity.this);
 			}
         	
         });
  //       m_alarmList = (ListView) findViewById(R.id.alarmList);
         
         
         tlm.setOnClickListener(new OnClickListener(){
 			@Override
 			public void onClick(View v) {
 				// TODO Auto-generated method stub
 				 Bundle bundle = new Bundle();
 	                bundle.putInt("alarm_position", 0);
 	                Intent intent = new Intent(boxGameAlarmActivity.this, SetAlarmActivity.class);
 	                intent.putExtras(bundle);
 	                boxGameAlarmActivity.this.startActivity(intent);
 			}
         	
         });
         
         imageView_cirtl.setOnClickListener(new OnClickListener(){
         	int position  = 0;
 			@Override
 			public void onClick(View v) {
 				// TODO Auto-generated method stub
 				boolean state = !m_listBean.get(position).getEnabled();
 				m_listBean.get(position).setEnabled(state);
 				setButtonState(position,state);
                refreshData(position);
 			}
         	
         });
         
         trm.setOnClickListener(new OnClickListener(){
 			@Override
 			public void onClick(View v) {
 				// TODO Auto-generated method stub
 				 Bundle bundle = new Bundle();
 	                bundle.putInt("alarm_position", 1);
 	                Intent intent = new Intent(boxGameAlarmActivity.this, SetAlarmActivity.class);
 	                intent.putExtras(bundle);
 	                boxGameAlarmActivity.this.startActivity(intent);
 			}
         	
         });
     
     imageView_cirtr.setOnClickListener(new OnClickListener(){
     	int position  = 1;
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean state = !m_listBean.get(position).getEnabled();
 				m_listBean.get(position).setEnabled(state);
 				setButtonState(position,state);
             refreshData(position);
			}
     	
     });
     
     blm.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Bundle bundle = new Bundle();
	                bundle.putInt("alarm_position", 2);
	                Intent intent = new Intent(boxGameAlarmActivity.this, SetAlarmActivity.class);
	                intent.putExtras(bundle);
	                boxGameAlarmActivity.this.startActivity(intent);
			}
     	
     });
    
     imageView_cirbl.setOnClickListener(new OnClickListener(){
     	int position  = 2;
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean state = !m_listBean.get(position).getEnabled();
 				m_listBean.get(position).setEnabled(state);
 				setButtonState(position,state);
             refreshData(position);
			}
     	
     });
     
     brm.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Bundle bundle = new Bundle();
	                bundle.putInt("alarm_position", 3);
	                Intent intent = new Intent(boxGameAlarmActivity.this, SetAlarmActivity.class);
	                intent.putExtras(bundle);
	                boxGameAlarmActivity.this.startActivity(intent);
			}
     	
     });
     imageView_cirbr.setOnClickListener(new OnClickListener(){
     	int position  = 3;
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean state = !m_listBean.get(position).getEnabled();
 				m_listBean.get(position).setEnabled(state);
 				setButtonState(position,state);
             refreshData(position);
			}
     	
     });
         
    }

	private void setUI() {
		m_listBean = AlarmProvider.geInstance(this).getAlarms();
		
		Form f = Form.getForm(m_listBean.get(0).getForm());
		if(f!=null){
			tlm.setImageResource(f.getMainPic());
			Log.e("aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaa");
		}
		tvtlname.setText(m_listBean.get(0).getName());
		tvtltime.setText(Utils.getCalendarTime(m_listBean.get(0).getTime()));
		tvtlring.setText(Utils.formatRingMode(m_listBean.get(0).getRingMode()));

		tvtrname.setText(m_listBean.get(1).getName());
		tvtrtime.setText(Utils.getCalendarTime(m_listBean.get(1).getTime()));
		tvtrring.setText(Utils.formatRingMode(m_listBean.get(1).getRingMode()));

		tvblname.setText(m_listBean.get(2).getName());
		tvbltime.setText(Utils.getCalendarTime(m_listBean.get(2).getTime()));
		tvblring.setText(Utils.formatRingMode(m_listBean.get(2).getRingMode()));

		tvbrname.setText(m_listBean.get(3).getName());
		tvbrtime.setText(Utils.getCalendarTime(m_listBean.get(3).getTime()));
		tvbrring.setText(Utils.formatRingMode(m_listBean.get(3).getRingMode()));

		setButtonState();

		if (AlarmProvider.geInstance(this).isSleepAlarmEnable(0)) {
			m_btStopSleepAlarm.setEnabled(true);
		} else {
			m_btStopSleepAlarm.setEnabled(false);
		}
		//
	}

    private void setButtonState(){
    	for(int i=0;i<4;i++){
    		setButtonState(i,m_listBean.get(i).getEnabled());
    	}
    }
    private void setButtonState(int i,boolean state){
    	ImageView iv = null;
    	switch(i){
    	case 0:{
    		iv= imageView_cirtl;
    		break;
    	}
    	case 1:{
    		iv = imageView_cirtr;
    		break;
    		
    	}
    	case 2:{
    		iv= imageView_cirbl;
    		break;
    	}
    	case 3:{
    		iv=imageView_cirbr;
    		break;
    	}
    	default : return;
    	
    	}
    	int alpha  = 255;
    	if(state==false){
    		alpha = 0;
    	}
    	iv.setAlpha(alpha);
    	//iv.setAlpha(alpha)
   // 	iv.setVisibility(View.INVISIBLE);
    	iv.invalidate();
    }

    @Override
    public void refreshData() {
        AlarmProvider ap = AlarmProvider.geInstance(this.getApplicationContext());
        for (int i = 0; i <m_listBean.size(); i++) {
            ap.setAlarmEnable(i, (m_listBean.get(i).isEnabled()));
        }

       
            AlarmSet.setAlarm(this);
       
    }
    public void refreshData(int i) {
    	  AlarmProvider ap = AlarmProvider.geInstance(this.getApplicationContext());
          ap.setAlarmEnable(i, (m_listBean.get(i).isEnabled()));
          AlarmSet.setAlarm(this);
       
    }

     @Override
    public void refreshUI() {
       
    	 setUI();
    }
}
